<?php
/**
 * 后台主界面
 */
declare (strict_types = 1);

namespace app\admin\controller\admin;

use app\admin\model\admin\AdminMessage;
use app\admin\model\admin\AdminUser;
use app\admin\model\admin\AdminUserMessage;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Index extends Base
{
    /**
     * 后台主页面
     */
    public function index()
    {
        return View::fetch();
    }

    //首页内容
    public function home()
    {
        return View::fetch('console');
    }

    
    //菜单
    public function menu()
    {
        $uid = Session::get('admin.id');
        $menu = get_tree(Cache::get("admin:{$uid}:menu"));
        return json($menu);
    }

    //修改密码
    public function pass()
    {
        $postData = Request::post();
        if ($postData){

            if (!$postData['old_password'] || !$postData['password'] || !$postData['again_password']){
                $this->error('参数缺失');
            }
           
            if (strlen($postData['password']) < 6){
                $this->error('密码过短(6位字符及以上)');
            }

            if ($postData['password'] != $postData['again_password']){
                $this->error('两次密码不一致');
            }
            $user = AdminUser::find(Session::get('admin.id'));
            $userPassword = $user->value('password');
            $oldPass = md5(trim($postData['old_password']));
            if ($userPassword != substr($oldPass,8,16) ){
                return $this->error('原密码错误');
            }
            $newPassword = md5(trim($postData['password']));
            $user->password = substr($newPassword,8,16);
            if (!$user->save()){
                return $this->error('修改失败');
            }
            $user->logout();
            $this->jsonApi('修改成功',200,APP_DS_PHP.'/index');
        }
        return View::fetch();
    }


    public function cache()
    {
        $this->rm();
        $this->jsonApi('清理成功');
    }

    public function adminMessage()
    {
        $adminId = Session::get('admin.id');
        $msgIdArr = AdminUserMessage::where(['user_id' => $adminId, 'is_see' => '0'])
            ->column('msg_id');
        $msgData = AdminMessage::where('id', 'in', $msgIdArr)
            ->order('create_time', 'DESC')
            ->field('id,title,context,create_time')
            ->select();
        $data = [['id' => 0, 'title' => '通知', 'children' => $msgData]];
        $this->success('','', $data);
    }

    public function seeMsg()
    {
        if (!Request::isAjax() || !Request::isPost()){
            $this->error('请求错误');
        }
        $postData = Request::post();
        if (!array_key_exists('msg_id', $postData)){
            $this->error('参数缺失');
        }
        $res = Db::table('admin_msg_user')->where(['user_id' => Session::get('admin.id'), 'msg_id' => $postData['msg_id'], 'is_see' => '2'])->update(['is_see' => 1]);
        if (!$res){
            $this->error('修改消息状态失败');
        }
        $this->success('');
    }


}
